import { requestData } from "../../config/requestData"
import { defaultUrl, hotDetailUrl, searchUrl } from '../../config/api'
let  throttleFlag = false //节流
// pages/search/search.js
Page({
    /**
     * 页面的初始数据
     */
    data: {
        realkeyword: '', //输入框提示问题
        inputValue: '', //提示框
        historyList: [], //搜索历史
        timer: null,
        historyDataList: [], //搜索过历史
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.loadingPlaceholderData()
        this.loadingHotData()
        let historyDataList = wx.getStorageSync('historyDataList')
        if (historyDataList) {
            this.setData({
                historyDataList
            })
        }
    },
    // 方法
    hanldeInputValue(e) {
        const { value } = e.detail
        this.setData({
            inputValue: value
        })
        this.data.historyDataList.push(value)
        if(throttleFlag) return
        throttleFlag = true
        this.loadingHistoryData()
        this.data.timer = setTimeout(() => {
            throttleFlag = false
        }, 500)
        wx.setStorageSync('historyDataList', this.data.historyDataList)
    },
    // 清除
    handleClear(){ 
        this.setData({
            inputValue: '',
            historyList: []
        })
    },
    handleDelete () {
        this.setData({
            historyDataList: []
        })
        wx.clearStorage()
    },
    // 加载中
    async loadingPlaceholderData () {
        const options = {
            url: defaultUrl
        }
        const res = await requestData(options)
        const { code, data } = res
        if (code === 200) {
            this.setData({
               realkeyword: data.realkeyword
            })
        }
    },
    async loadingHotData () {
        const options = {
            url: hotDetailUrl
        }
        const res = await requestData(options)
        const { code, data }  = res
        if (code === 200) {
            this.setData({
                hotList: data
            })
        }
    },
    async loadingHistoryData () {
        const options = {
            url: searchUrl,
            data: {
                keywords: this.data.inputValue,
                limit: 10
            }
        }
        const res = await requestData(options)
        const { code, result } = res
        if (code === 200) {
            this.setData({
                historyList: result.songs
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        clearTimeout(this.data.timer)
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})